define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'skugoods'], function($, undefined, Backend, Table, Form, Sku) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/skugoods/index' + location.search,
                    add_url: 'shop/skugoods/add',
                    edit_url: 'shop/skugoods/edit',
                    del_url: 'shop/skugoods/del',
                    multi_url: 'shop/skugoods/multi',
                    table: 'jeek_shop_sku_goods',
                }
            });

            var table = $("#table");

            $('.btn-add').data('area', ['80%', '80%']);
            table.on('post-body.bs.table', function(e, setting, json, xhr) {
                $('.btn-editone,.btn-edit').data('area', ['80%', '80%']);
            });

            // 快捷搜索默认提示
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function() { return "名称"; };

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                showExport: false,
                columns: [
                    [
                        { checkbox: true },
                        {
                            field: 'id',
                            title: __('Id'),
                            width: '60px',
                            searchable: false,
                            sortable: true,
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE %...%',
                        },
                        {
                            field: 'category.name',
                            title: __('Category'),
                            operate: 'LIKE %...%',
                            formatter: Table.api.formatter.search,
                        },
                        {
                            field: 'price',
                            title: __('Price'),
                            formatter: function(v, i, r) {
                                return '<c class="text-success">' + v + '</c>';
                            },
                            searchable: false,
                            sortable: false
                        },

                        {
                            field: 'image',
                            title: __('Image'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            searchable: false
                        },
                        {
                            field: 'images',
                            title: __('Images'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images,
                            searchable: false
                        },
                        {
                            field: 'sales',
                            title: __('Sales'),
                            formatter: function(v, i, r) {
                                return '<c class="text-danger">' + v + '</c>';
                            },
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'comments',
                            title: __('Comments'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'hits',
                            title: __('Hits'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'likes',
                            title: __('Likes'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'weigh',
                            title: __('Weigh'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.toggle
                        },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");
            // 快捷搜索默认提示
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function() { return "名称"; };
            $('.btn-add').data('area', ['80%', '80%']);
            table.on('post-body.bs.table', function(e, setting, json, xhr) {
                $('.btn-editone,.btn-edit').data('area', ['80%', '80%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: 'shop/skugoods/recyclebin' + location.search,
                pk: 'id',
                sortName: 'deletetime',
                showExport: false,
                columns: [
                    [
                        { checkbox: true },
                        {
                            field: 'id',
                            title: __('Id'),
                            width: '60px',
                            searchable: false,
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE %...%',
                        },
                        {
                            field: 'image',
                            title: __('Image'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            searchable: false
                        },
                        {
                            field: 'sales',
                            title: __('Sales'),
                            formatter: function(v, i, r) {
                                return '<c class="text-danger">' + v + '</c>';
                            },
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'comments',
                            title: __('Comments'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'hits',
                            title: __('Hits'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'likes',
                            title: __('Likes'),
                            searchable: false,
                            sortable: true
                        },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime,
                            sortable: true,
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/skugoods/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/skugoods/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            var d = { groups: [], items: [], products: [] }
            Sku._init(d);
            Controller.api.bindevent();
        },
        edit: function() {
            Sku._init(Config.skus);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});